//
//  PersonCenterViewController.m
//  MobileOffice
//
//  Created by Appel on 16/10/11.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "PersonCenterViewController.h"
#import "PersonCenterTableViewCell.h"
#import "MyInfoViewController.h"
#import "SystemSettingViewController.h"
#import "PersonInfoDetailViewController.h"

#define getUserInfo @"getUserInfo"

@interface PersonCenterViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *_imageStr;
}
@end

@implementation PersonCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBackNavigation];
    [self setNavigationTitle:@"系统设置"];
    [self setRightBackNavigation];
    [self viewInit];
    if (!self.infoModel) {
        [self userInfoRequest];
    }
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userHeadImageChanged) name:@"userHeadImageChanged" object:nil];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - 通知

- (void)userHeadImageChanged{
    [self userInfoRequest];
}
#pragma mark - 页面初始化
- (void)viewInit{
    [self setLeftBackNavigation];
    [self setNavigationTitle:@"个人中心"];
    CGFloat imageWidth;//头像距上左边距
    CGFloat backViewHeight;//backView的高度
    CGFloat labelHeight;
    //根据屏幕大小设置间隔大小
    if (IPHONE6P){
        imageWidth=50;
        backViewHeight=130+2*imageWidth;
        labelHeight=5;
    }else if(IPHONE6){
        imageWidth=40;
        backViewHeight=130+2*imageWidth;
        labelHeight=3;
    }else{
        imageWidth=30;
        backViewHeight=130+2*imageWidth;
        labelHeight=0;
    }
    //设置view坐标
    _backView.frame=CGRectMake(0, 0, IPHONE_WIDTH, backViewHeight);
    _backImageView.frame=CGRectMake(0, 0, _backView.width, _backView.height);
    _backImageView.image=[UIImage imageNamed:@"k_bg_person@2x"];
    
    _headImageView.frame=CGRectMake(imageWidth, imageWidth, 80, 80);
    //切圆
    _headImageView.layer.masksToBounds=YES;
    _headImageView.layer.cornerRadius=_headImageView.width/2;
    //重新设置所有view的frame
    _nameLabel.frame=CGRectMake(_headImageView.x+_headImageView.width+imageWidth, 10+imageWidth/2-5, IPHONE_WIDTH-(_headImageView.x+_headImageView.width+imageWidth), 21);
    _positionLabel.frame=CGRectMake(_nameLabel.x, _nameLabel.y+_nameLabel.height+labelHeight, _nameLabel.width, _nameLabel.height);
    _departmentLabel.frame=CGRectMake(_nameLabel.x, _positionLabel.y+_positionLabel.height+labelHeight, _nameLabel.width, _nameLabel.height);
    _phoneNumbelLabel.frame=CGRectMake(_nameLabel.x, _departmentLabel.y+_departmentLabel.height+labelHeight, _nameLabel.width, _nameLabel.height);
    _emailLabel.frame=CGRectMake(_nameLabel.x, _phoneNumbelLabel.y+_phoneNumbelLabel.height+labelHeight, _nameLabel.width, _nameLabel.height);
    
    //半透明
    _secondView=[[UIView alloc]initWithFrame:CGRectMake(0, _backView.height-50, _backView.width, 50)];
    _secondView.backgroundColor=[UIColor blackColor];
    _secondView.alpha=0.4;
    [_backView addSubview:_secondView];
    
    self.view.backgroundColor=COLOR_HIGHBLUE;
    _myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, _backView.y+_backView.height, IPHONE_WIDTH, 220) style:UITableViewStylePlain];
    _myTableView.bounces=NO;
    _myTableView.delegate=self;
    _myTableView.backgroundColor=COLOR_MIDBLUE;
    _myTableView.dataSource=self;
    _myTableView.scrollEnabled=NO;
    _myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTableView];
    
    _loginOutButton.frame=CGRectMake(20, IPHONE_HEIGHT-80-64, IPHONE_WIDTH-40, 40);
    _loginOutButton.layer.cornerRadius=5;
    
    //设置用户部分信息
    NSArray *titleArr=@[@"考核",@"绩效",@"加班",@"请假"];
    NSArray *bodyArr=@[@"0分",@"0分",@"0天",@"0次"];
    for (int i=0; i<titleArr.count; i++) {
        UILabel *label=[MyControl creatLabelWithFrame:CGRectMake((IPHONE_WIDTH/titleArr.count)*i, _backView.height-25, IPHONE_WIDTH/titleArr.count, 20) text:titleArr[i]];
        label.font=FONT_14;
        label.textColor=[UIColor whiteColor];
        label.textAlignment=NSTextAlignmentCenter;
        [_backView addSubview:label];
        
        UILabel *bodyLabel=[MyControl creatLabelWithFrame:label.frame text:bodyArr[i]];
        [bodyLabel changeView_Y:_backView.height-45];
        bodyLabel.font=FONT_16;
        bodyLabel.textAlignment=NSTextAlignmentCenter;
        bodyLabel.textColor=COLOR_SKYBLUE;
        bodyLabel.tag=200+i;
        if (i==0) {
            //bodyLabel.text=[NSString stringWithFormat:@"%.1f",[[MyControl getUserAccXf] floatValue]];
        }
        [_backView addSubview:bodyLabel];
        //添加分割线
        if (i!=0) {
            UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(IPHONE_WIDTH/titleArr.count*i, _backView.height-45, 1, 40)];
            line.backgroundColor=COLOR_SKYBLUE;
            [_backView addSubview:line];
        }
    }
    
    //查看个人绩效信息详情
    UIButton *infoButton=[MyControl creatButtonWithFrame:CGRectMake(0, 0, _backView.width, _backView.height) target:self sel:@selector(infoButtonClick) tag:190 image:nil title:@""];
    [_backView addSubview:infoButton];
    
    //如果传过来数据则直接使用
    [_headImageView setImageWithURL:[NSURL URLWithString:_infoModel.img] placeholderImage:[UIImage imageNamed:@"k_portrait@2x"]];
    _imageStr=[_infoModel.img copy];
    _nameLabel.text=_infoModel.empName?_infoModel.empName:@"";
    _departmentLabel.text=[NSString stringWithFormat:@"职位：%@",_infoModel.departName?_infoModel.departName:@""];
    _positionLabel.text=[NSString stringWithFormat:@"部门：%@",_infoModel.postName?_infoModel.postName:@""];
    _phoneNumbelLabel.text=[NSString stringWithFormat:@"电话：%@",_infoModel.phoneNumber?_infoModel.phoneNumber:@""];
    _emailLabel.text=[NSString stringWithFormat:@"邮箱：%@",_infoModel.mailbox?_infoModel.mailbox:@""];
    for (int i=0; i<4; i++) {
        UILabel *label=(UILabel *)[_backView viewWithTag:200+i];
        if (i==0) {
            //考核
            NSString *performanceRanking=nil;
            if (_infoModel.performanceRanking.integerValue>0) {
                performanceRanking=[NSString stringWithFormat:@"第%@名",_infoModel.performanceRanking];
            }else{
                performanceRanking=@"0次";
            }
            label.text=performanceRanking;
        }else if (i==1){
            //绩效
            label.text=[NSString stringWithFormat:@"%@分",_infoModel.assessGrade];
        }else if (i==2){
            label.text=[NSString stringWithFormat:@"%@次",_infoModel.overtimeCount];
        }else{
            label.text=[NSString stringWithFormat:@"%@次",_infoModel.vacateTotalCount];
        }
    }
}

#pragma mark - 个人绩效信息详情
- (void)infoButtonClick{
    PersonInfoDetailViewController *person=[PersonInfoDetailViewController new];
    [self.navigationController pushViewController:person animated:YES];
}

#pragma mark -tableViewDelagate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }else{
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   NSString *cellID=@"PersonCenterTableViewCell";
    PersonCenterTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell=[[PersonCenterTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        //右边小箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.contentView.backgroundColor=[UIColor clearColor];
        cell.backgroundColor=[UIColor clearColor];
    }
    if (indexPath.section==0) {
        cell.myTitle.text=@"私       密";
        cell.headImageView.image=[UIImage imageNamed:@"k_ic_tssz@2x"];
    }else{
        if (indexPath.row==0) {
            cell.myTitle.text=@"个人设置";
            cell.headImageView.image=[UIImage imageNamed:@"k_ic_tcdl@2x"];
            UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(0, 59, IPHONE_WIDTH, 1)];
            line.backgroundColor=COLOR_HIGHBLUE;
            [cell.contentView addSubview:line];
        }else{
            cell.myTitle.text=@"系统设置";
            cell.headImageView.image=[UIImage imageNamed:@"k_ic_yjfk@2x"];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0;
    }else{
        return 30;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==1) {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 30)];
        view.backgroundColor=COLOR_HIGHBLUE;
        return view;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        
    }else{
        if (indexPath.row==0) {
            //个人设置
            MyInfoViewController *info=[MyInfoViewController new];
            info.imageStr=_imageStr;
            [self.navigationController pushViewController:info animated:YES];
        }else{
            //系统设置
            SystemSettingViewController *system=[SystemSettingViewController new];
            [self.navigationController pushViewController:system animated:YES];
        }
    }
}


#pragma mark -用户信息请求
- (void)userInfoRequest{

//    //请求
    NSDictionary *dict=@{
//                         @"password":_passWordTextField.text,
//                         @"username":_userTextField.text,
                         @"sessionId":[LZXHelper getSessionID],
                         //                        @"reqStr":@"weaqtfaw"
                         };
//    //    NSMutableDictionary *dict2=[[NSMutableDictionary alloc]init];
//    //    [dict2 setObject:dict forKey:@"content"];
//    //    [dict2 setObject:@"" forKey:@"sessionId"];
    [LZXHelper postLoadViewWithText:HUD_PLEASEWAIT View:self.view];
//    
//   字典转json
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //拼接soap字符串
    NSString* soap = [NSString stringWithFormat:@"<sessionId>%@</sessionId>\n<content>%@</content>",[LZXHelper getSessionID],str];
    
    [HttpRequest SOAPData:[LZXHelper getServerURL] SoapMthod:getUserInfo soapBody:soap success:^(id responseObject) {
        [LZXHelper hideLoadViewWithView:self.view];

        _infoModel=[[UserInfoModel alloc]initWithResponse:responseObject];
        
        if ([_infoModel.code isEqualToString:@"200"]) {
            [_headImageView setImageWithURL:[NSURL URLWithString:_infoModel.img] placeholderImage:[UIImage imageNamed:@"k_portrait@2x"]];
            _imageStr=[_infoModel.img copy];
            _nameLabel.text=_infoModel.empName?_infoModel.empName:@"";
            _departmentLabel.text=[NSString stringWithFormat:@"职位：%@",_infoModel.departName?_infoModel.departName:@""];
            _positionLabel.text=[NSString stringWithFormat:@"部门：%@",_infoModel.postName?_infoModel.postName:@""];
            _phoneNumbelLabel.text=[NSString stringWithFormat:@"电话：%@",_infoModel.phoneNumber?_infoModel.phoneNumber:@""];
            _emailLabel.text=[NSString stringWithFormat:@"邮箱：%@",_infoModel.mailbox?_infoModel.mailbox:@""];
            for (int i=0; i<4; i++) {
                UILabel *label=(UILabel *)[_backView viewWithTag:200+i];
                if (i==0) {
                    //考核
                    NSString *performanceRanking=nil;
                    if (_infoModel.performanceRanking.integerValue>0) {
                        performanceRanking=[NSString stringWithFormat:@"第%@名",_infoModel.performanceRanking];
                    }else{
                        performanceRanking=@"0次";
                    }
                    label.text=performanceRanking;
                }else if (i==1){
                    //绩效
                    label.text=[NSString stringWithFormat:@"%@分",_infoModel.assessGrade];
                }else if (i==2){
                    label.text=[NSString stringWithFormat:@"%@次",_infoModel.overtimeCount];
                }else{
                    label.text=[NSString stringWithFormat:@"%@次",_infoModel.vacateTotalCount];
                }
            }
        }else if([_infoModel.code isEqualToString:@"220"]){
            [LZXHelper postMessageWithText:_infoModel.msg View:self.view];
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (IBAction)loginOutClick:(id)sender {
    [LZXHelper postAlertViewControllerWithTitle:@"确定要退出此账号吗?" message:nil cancelButtonTitle:@"取消" otherButtonTitle:@"确定" cancelEvent:^{
        
    } otherEvent:^{
        [self.navigationController.viewControllers.firstObject dismissViewControllerAnimated:YES completion:nil];
    } viewController:self];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController]. ///test svn again
    // Pass the selected object to the new view controller. test svn
}
*/

@end
